//Requires Model,View and App to be loaded first
//Put Event-handlers and control-flow related stuff here
var gxMenuNeptuner = null;
var gxMenuSourceLine = null;

function Controller_init()
{
    initMenubar();
    initNoteLines();
    initComments();
    initMenus();

    var activeLines = $$('.active');
    if(activeLines.length < 1)
        activeLine=$('SL1');
    else
    {
        var n=activeLines.length;
        for(var i=0;i<n-1;i++){
             activeLines.pop().removeClassName('active');
        }
        activeLine = activeLines[0];
    }
    activeLine.scrollTo();
    
    activateLine();
    activateBookmarks();
    activateScraps();

    initFieldHandlers();
    initShortcuts();
}

codeLineMenuItems = [
  {name: 'Add Bookmark',className: 'edit',callback: addBookmark},
  {name: 'Show Notes',className: 'delete',callback: showNotes},
  {name: 'Hide Notes',className: 'delete', callback: hideNotes},
  {name: 'Remove Notes',className: 'delete', callback: removeNotes}
];

neptunerMenuItems = [
  {name: 'Goto Codebase Home',className: 'edit', callback: UBoat_gotoNeptunerHome},
  {name: 'Goto Submarine View',className: 'delete', callback: UBoat_gotoSubmarine},
  {separator: true},
  {name: 'About',className: 'save',callback: UBoat_about}
];

function initMenubar(){
    new ProtoFish('menuUBoat', '200', 'hover', true, true, true);
}

function initNoteLines(){
    for (i = 1; i <= nSourceLines; i++){
        var locationid='SL'+i;
        InPlaceNote.makeEditable( {
                type: 'textarea',
                id: locationid,
                save_url: 'edit.php'
        } );
        $(locationid).observe('click', onClickSourceLine)
    }
}

function initComments(){
    $$('.review').each(
        function (elmt){
            EditInPlace.makeEditable(
                    {
                    type: 'textarea',
                    id: elmt.id,
                    save_url: 'edit.php'
                    });
    });
}

function initMenuNeptuner(){
    gxMenuNeptuner = new Proto.Menu({
      selector: '#menuNeptuner', 
      className: 'protomenu firefox',
      menuItems: neptunerMenuItems, 
      activator: 'click'
    });
}

function initMenuSource(){
    gxMenuSourceLine =  new Proto.Menu({
      selector: '.slclicker', 
      className: 'protomenu firefox',
      menuItems: codeLineMenuItems, 
      activator: 'click'
    });

}

function onClickSourceLine(ev){
    // Code to respond to each click goes here
    deactivateLine();
    activeLine = findTargetLine(ev);
    activateLine();
    //alert(activeLine.identify());
}

function activateBookmarks(){
    $$('.bookmark').each(
        function (elmt){
        EditInPlace.makeEditable( {
                type: 'textarea',
                id: elmt.id,
                save_url: 'edit.php',
                is_wrapped:true
        } );
        }
    );
}

function activateScraps(){
    $$('.scrap').each(
        function (elmt){
        EditInPlace.makeEditable( {
                type: 'textarea',
                id: elmt.id,
                save_url: 'edit.php',
                is_wrapped:true
        } );
        }
    );
}

function initFieldHandlers()
{
    Event.observe('inpLine', 'keypress', KeyPress_LineEntry);
    Event.observe('textareaScrap', 'keypress', KeyPress_ScrapEntry);
    Event.observe('textareaScrap', 'click', Click_ScrapEntry);

}

function KeyPress_LineEntry(evt){
    switch(evt.keyCode){
        case Event.KEY_RETURN:
            gotoLine();
            evt.stop();
            break;
    }
}

function KeyPress_ScrapEntry(evt){
    switch(evt.keyCode){
        case Event.KEY_RETURN:
            addScrap();
            evt.stop();
            $('textareaScrap').select();
            break;

        default:
            $('textareaScrap').removeClassName('notedited');
            break;
    }
}

function Click_ScrapEntry(){
    var field = $('textareaScrap');
    if(field.hasClassName('notedited'))
        $('textareaScrap').select();
}

function initShortcuts(){
    shortcut.add("Ctrl+S",UBoat_saveFile,
        {'propagate':false,'type':'keypress','target':document});
}

function initMenus(){
    initMenuNeptuner();
    initMenuSource();
}